
//
//  UIButton+Extension.swift
//  Weibo-Swift
//
//  Created by law on 2018/6/1.
//  Copyright © 2018年 JKing. All rights reserved.
//

import Foundation

// extension 类似于 OC 中的分类，作用是对现有的类`扩展`方法！
// 扩展中不允许编写指定构造函数！只能添加便利构造函数，(OC中可以定义类函数)
// extension 中同样不能定义`存储型`的属性！
extension UIButton { 
    
    /// 便利构造函数 : 快速创建一个UIButton
    ///
    /// - Parameters:
    ///   - imageName: imageName
    ///   - backImageName: backImageName
    convenience init(imageName: String, backImageName: String?) {
        self.init()
        
        setImage(UIImage(named: imageName), for: .normal)
        setImage(UIImage(named: imageName + "_highlighted"), for: .normal)
        
        if backImageName != nil {
            setBackgroundImage(UIImage(named: backImageName!), for: .normal)
            setBackgroundImage(UIImage(named: backImageName! + "_highlighted"), for: .highlighted)
        }
        
        sizeToFit()
    }
    
    /// 便利构造函数 : 快速创建一个UIButton
    ///
    /// - Parameters:
    ///   - title: title
    ///   - fontSize: fontSize
    ///   - normalColor: normalColor
    ///   - highlightedColor: highlightedColor
    ///   - imageName: imageName
    ///   - backImageName: backImageName
    convenience init(title: String, fontSize: CGFloat, normalColor: UIColor?, highlightedColor: UIColor?, imageName: String?,  backgroundImageName: String?) {
        self.init()
        
        setTitle(title, for: .normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        if normalColor != nil {
            setTitleColor(normalColor!, for: .normal)
        }
        
        if highlightedColor != nil {
            setTitleColor(highlightedColor!, for: .highlighted)
        }
        
        if imageName != nil {
            setImage(UIImage(named: imageName!), for: .normal)
        }
        
        if backgroundImageName != nil {
            setBackgroundImage(UIImage(named: backgroundImageName!), for: .normal)
        }
    }
    
    /// 便利构造函数 : 快速创建一个UIButton
    ///
    /// - Parameters:
    ///   - title: title
    ///   - fontSize: fontSize
    ///   - normalColor: normalColor
    ///   - highlightedColor: highlightedColor
    ///   - backImageName: backImageName
    convenience init(title: String, fontSize: CGFloat, normalColor: UIColor, highlightedColor: UIColor, backgroundImageName: String?) {
        self.init(title: title, fontSize: fontSize, normalColor: normalColor, highlightedColor: highlightedColor, imageName: nil, backgroundImageName: backgroundImageName)
    }
    
    /// 便利构造函数 : 快速创建一个UIButton
    ///
    /// - Parameters:
    ///   - title: title
    ///   - fontSize: fontSize
    ///   - color: color
    ///   - imageName: imageName
    convenience init(title: String, fontSize: CGFloat, color: UIColor, imageName: String) {
        self.init(title: title, fontSize: fontSize, normalColor: color, highlightedColor: nil, imageName: imageName, backgroundImageName: nil)
    }
    
    
    /// 便利构造函数 : 快速创建一个UIButton
    ///
    /// - Parameters:
    ///   - title: title
    ///   - fontSize: fontSize
    ///   - normalColor: normalColor
    ///   - highlightedColor: highlightedColor
    convenience init(title: String, fontSize: CGFloat, normalColor: UIColor, highlightedColor: UIColor) {
        self.init(title: title, fontSize: fontSize, normalColor: normalColor, highlightedColor: highlightedColor, backgroundImageName: nil)
    }
    
}
